/*
 * Copyright 2007 Stefan Arentz <http://stefan.arentz.nl>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sateh.spaces.simple;

import com.sateh.spaces.Lease;
import com.sateh.spaces.UnknownLeaseException;

import java.util.UUID;

public class SimpleSpaceLease implements Lease
{
    /**
     * The time on the local system that this lease was created.
     */

    private long creationTime = System.currentTimeMillis();

    SimpleSpaceLease(SimpleSpace space, UUID objectId, long timeToLive)
    {
        this.space = space;
        this.objectId = objectId;
        this.timeToLive = timeToLive;
    }

    private SimpleSpace space;

    public SimpleSpace getSpace()
    {
        return space;
    }

    private UUID objectId;

    public UUID getObjectId()
    {
        return objectId;
    }

    private long timeToLive;

    public long getTimeToLive()
    {
        return timeToLive;
    }

    public long getExpirationTime()
    {
        return creationTime + timeToLive;
    }

    /**
     * @see com.sateh.spaces.Lease#cancel() 
     */

    public void cancel() throws UnknownLeaseException
    {
        if (!space.cancelLeaseForObjectWithId(objectId)) {
            throw new UnknownLeaseException();
        }
    }

    /**
     * @see com.sateh.spaces.Lease#renew(long) 
     */

    public void renew(long duration) throws UnknownLeaseException
    {
        if (!space.renewLeaseForObjectWithId(objectId, duration)) {
            throw new UnknownLeaseException();
        }
    }
}
